package com.fos.BluetoothStory.data;

import android.content.Context;
import android.content.Intent;

import com.fos.BluetoothStory.GlobalApplication;
import com.fos.BluetoothStory.model.MusicBean;
import com.fos.BluetoothStory.util.AppConstance;

import java.util.ArrayList;

public class TFMusicState {
	public static final String			ACTION_VOLUME_CHANGDE			= "ACTION_VOLUME_CHANGDE";
	public static final String			ACTION_TFPLAY_MODE_CHANGDE		= "ACTION_TFPLAY_MODE_CHANGDE";
	public static final String			ACTION_USBPLAY_MODE_CHANGDE		= "ACTION_USBPLAY_MODE_CHANGDE";
	public static final String			ACTION_PLAY_PAUSE_CHANGDE		= "ACTION_STATE_CHANGDE";
	public static final String			ACTION_NOW_PLAYING_CHANGDE		= "ACTION_NOW_PLAYING_CHANGDE";
	public static final String			ACTION_BT_SINGAL_CHANGED		= "ACTION_BT_SINGAL_CHANGED";
	public static final String			ACTION_BT_VOLTAGE_CHANGED		= "ACTION_BT_VOLTAGE_CHANGED";
	public static final String			ACTION_SWITCH_TO_YINXIANG		= "ACTION_SWITCH_TO_YINXIANG";
	public static final String			ACTION_SWITCH_TO_USB			= "ACTION_SWITCH_TO_USB";
	public static final String			ACTION_SWITCH_TO_TFCARD			= "ACTION_SWITCH_TO_TFCARD";
	public static final String			ACTION_READY_SWITCH_TO_USB		= "ACTION_READY_SWITCH_TO_USB";
	public static final String			ACTION_READY_SWITCH_TO_TFCARD	= "ACTION_READY_SWITCH_TO_TFCARD";
	public static final String			ACTION_TF_MUSIC_LIST_CHANGED	= "ACTION_TF_MUSIC_LIST_CHANGED";
	public static final String			ACTION_EQMODE_CHANGED			= "ACTION_EQMODE_CHANGED";
	public static final String			ACTION_SWITCH_TO_LINEIN			= "ACTION_SWITCH_TO_LINEIN";
    public static final String			ACTION_ALERT_TOAST_MSG			= "ACTION_ALERT_TOAST_MSG";
    public static final String			EXTRA_TOSAT_MSG					= "EXTRA_TOSAT_MSG";
    public static final String			ACTION_ADD_MUSIC_BEAN			= "ACTION_ADD_MUSIC_BEAN";
    public static final String			EXTRA_MUSIC_NO			        = "EXTRA_MUSIC_NO";
    public static final String			EXTRA_MUSIC_NAME			    = "EXTRA_MUSIC_NAME";



	private static TFMusicState			mState;
	private static Context				mContext;
	public static ArrayList<MusicBean> musicList						= new ArrayList<MusicBean>();		// 音乐列表
	private static int					tfPlayMode						= -1;
	private static int					usbPlayMode						= -1;
	private boolean						isPause							= false;
	private int							playVolume						= 4;
	private static int					signal							= -1;
	private int							playingNo;
	private String						playingName;
	private int							voltage							= -1;
	private int							eqYinxiang;
	private int							eqUSB;
	private int							eqLINEIN;
	private int							eqTFcard;
	private MusicBean playMusic;//当前播放的音乐

	public synchronized static TFMusicState getInstance() {
		if (mState == null) {
			mState = new TFMusicState();
			mContext = GlobalApplication.application;
		}

		return mState;
	}

	private TFMusicState() {

	}

    public void sendAddBroadcast(MusicBean obj){
        Intent intent = new Intent(ACTION_ADD_MUSIC_BEAN);
        intent.putExtra(EXTRA_MUSIC_NAME,obj.title);
        intent.putExtra(EXTRA_MUSIC_NO,obj.id);
        mContext.sendBroadcast(intent);
    }

	public synchronized void addMusic(MusicBean obj) {
        sendAddBroadcast(obj);
        /*
		if (musicList.size() == 0) {
			musicList.add(obj);
			mContext.sendBroadcast(new Intent(ACTION_TF_MUSIC_LIST_CHANGED));
			return;
		}

		for (int i = 0; i < musicList.size(); i++) {
			MusicBean mb = musicList.get(i);

			if (mb.getNo() == obj.getNo()) {
				return;
			}

			if (obj.getNo() < mb.getNo()) {
				musicList.add(i, obj);
				mContext.sendBroadcast(new Intent(ACTION_TF_MUSIC_LIST_CHANGED));
				return;
			}
		}

		musicList.add(obj);
		mContext.sendBroadcast(new Intent(ACTION_TF_MUSIC_LIST_CHANGED));
		*/
	}

	public synchronized ArrayList<MusicBean> getMusicList() {

		return musicList;
	}

	public int getMusicNum() {
		if (musicList != null) {
			return musicList.size();
		}

		return 0;
	}

	// 0x00：正常播放 0x01：单曲循环 0x02：循环播放 0x03：随机播放
	public synchronized void setTFPlayMode(int mode) {
		mode = mode % 4;
		if (tfPlayMode != mode) {
			tfPlayMode = mode;
			mContext.sendBroadcast(new Intent(ACTION_TFPLAY_MODE_CHANGDE));
		}
	}

	public synchronized int getTFPlayMode() {
		return tfPlayMode;
	}

	// 0x00：正常播放 0x01：单曲循环 0x02：循环播放 0x03：随机播放
	public synchronized void setUSBPlayMode(int mode) {
		mode = mode % 4;
		if (usbPlayMode != mode) {
			usbPlayMode = mode;
			mContext.sendBroadcast(new Intent(ACTION_USBPLAY_MODE_CHANGDE));
		}
	}

	public synchronized int getUSBPlayMode() {
		return usbPlayMode;
	}

	public synchronized void setIsPauseState(boolean b) {
		isPause = b;

		mContext.sendBroadcast(new Intent(ACTION_PLAY_PAUSE_CHANGDE));
	}

	public synchronized void setVolume(int volume) {
		playVolume = volume;
		mContext.sendBroadcast(new Intent(ACTION_VOLUME_CHANGDE));
	}

	public synchronized boolean getIsPauseState() {
		return isPause;
	}

	public synchronized int getVolume() {
		return playVolume;
	}

	public void setNowPlayNoAndName(int no, String name) {
		playingNo = no;
		playingName = name;
		mContext.sendBroadcast(new Intent(ACTION_NOW_PLAYING_CHANGDE));
	}

	public int getPlayingNo() {
		return playingNo;
	}

	public String getPlayingName() {
		return playingName;
	}

	public synchronized void setBtSignal(int btSignal) {
		if (signal != btSignal) {
			signal = btSignal;
		}

		mContext.sendBroadcast(new Intent(ACTION_BT_SINGAL_CHANGED));
	}

	public synchronized int getBtSignal() {
		return signal;
	}

	public void setVoltage(int btVoltage) {
		if (voltage != btVoltage) {
			voltage = btVoltage;
			mContext.sendBroadcast(new Intent(ACTION_BT_VOLTAGE_CHANGED));
		}

	}

	public int getBtVoltage() {
		return voltage;
	}

	public synchronized void clearMusicList() {
		if (musicList != null) {
			musicList.clear();
		}

		mContext.sendBroadcast(new Intent(ACTION_TF_MUSIC_LIST_CHANGED));
	}

	public MusicBean getMusicItem(int position) {
		if (musicList != null && position < musicList.size()) {
			return musicList.get(position);
		}

		return null;
	}

	/**
	 * 0x00 NORMAL 0x01 CLASSIC 0x02 JAZZ 0x03 POP 0x04 ROCK 0x05 EXBASE 0x06
	 * SOFT 0x07 自定义
	 */
	public void setEQMode(int mode) {
		// 0 音箱 1 USB 2LINEIN 3TFcard -1 默认值
		switch (AppConstance.sPlayState) {
		case 0:
			if (eqYinxiang != mode) {
				eqYinxiang = mode;
				mContext.sendBroadcast(new Intent(ACTION_EQMODE_CHANGED));
			}
			break;

		case 1:
			if (eqUSB != mode) {
				eqUSB = mode;
				mContext.sendBroadcast(new Intent(ACTION_EQMODE_CHANGED));
			}
			break;

		case 2:
			if (eqLINEIN != mode) {
				eqLINEIN = mode;
				mContext.sendBroadcast(new Intent(ACTION_EQMODE_CHANGED));
			}
			break;
		case 3:
			if (eqTFcard != mode) {
				eqTFcard = mode;
				mContext.sendBroadcast(new Intent(ACTION_EQMODE_CHANGED));
			}
			break;

		}

	}

	public int getEQMode() {
		// 0 音箱 1 USB 2LINEIN 3TFcard -1 默认值
		switch (AppConstance.sPlayState) {
		case 0:
			return eqYinxiang;

		case 1:
			return eqUSB;

		case 2:
			return eqLINEIN;

		case 3:
			return eqTFcard;
		}

		return 0;

	}

	public MusicBean getPlayMusic() {
		return playMusic;
	}
	
	public void setPlayMusic(MusicBean playMusic) {
		this.playMusic = playMusic;
	}
}
